﻿# ------------------------- #
# Storage Account Functions #
# ------------------------- #

# Add storage accounts
function Add-StorageAccounts([System.Xml.XmlElement]$subscription)
{
	if (!(Should-Run($subscription.storageAccounts))) {return}
	foreach ($account in $subscription.storageAccounts.storageAccount)
	{
	    if (!(Should-Run($account))) {continue}
		Write-Information "Adding storage account '$($account.name)' ... " -NoNewLine $true
	    if (Get-AzureStorageAccount -WarningAction Ignore | ? {$_.StorageAccountName -eq $account.name})
		{
			Write-Text "skipped."
		}
		else
		{
            # type="Standard_LRS|Standard_ZRS|Standard_GRS|Standard_RAGRS"
		    $operation = New-AzureStorageAccount -StorageAccountName $account.name -Location $account.location -Description $account.description -Type $account.type
		    if (!$? -or $operation.OperationStatus -ne "Succeeded") {throw "Storage account '$($account.name)' operation failed."}
			Write-Text "done."
		}
	    Add-StorageAccountContainers $account
        Assign-StorageAccountResourceGroup $account
	}
}

# Remove storage accounts
function Remove-StorageAccounts([System.Xml.XmlElement]$subscription)
{
	if (!(Should-Run($subscription.storageAccounts))) {return}
	foreach ($account in $subscription.storageAccounts.storageAccount)
	{
	    if (!(Should-Run($account))) {continue}
	    if (Get-AzureStorageAccount -WarningAction Ignore | ? {$_.StorageAccountName -eq $account.name})
		{
		    Remove-StorageAccountContainers $account
		    Write-Information "Removing storage account '$($account.name)' ... " -NoNewLine $true
		    $operation = Remove-AzureStorageAccount -StorageAccountName $account.name
		    if (!$? -or $operation.OperationStatus -ne "Succeeded") {throw "Storage account '$($account.name)' operation failed."}
			Write-Text "done."
		}
		else
		{
		    Write-Information "Removing storage account '$($account.name)' ... skipped."
		}
	}
}

# Add storage account containers
function Add-StorageAccountContainers($account)
{
	$key = (Get-AzureStorageKey -StorageAccountName $account.name).Primary
	$context = New-AzureStorageContext -StorageAccountName $account.name -StorageAccountKey $key
	foreach ($container in $account.containers.container)
	{
		Write-Information "Adding storage account '$($account.name)' container '$($container.name)' ... " -NoNewLine $true
	    if (Get-AzureStorageContainer -Context $context | ? {$_.Name -eq $container.name})
	    {
			Write-Text "skipped."
	    }
	    else
	    {
		    $result = New-AzureStorageContainer -Name $container.name -Permission Container -Context $context
		    if (!$? -or !$result) {throw "Storage account '$($account.name)' container '$($container.name)' operation failed."}
			Write-Text "done."
	    }	
    }
}

# Remove storage account containers
function Remove-StorageAccountContainers($account)
{
	$key = (Get-AzureStorageKey -StorageAccountName $account.name).Primary
	$context = New-AzureStorageContext -StorageAccountName $account.name -StorageAccountKey $key
	foreach ($container in $account.containers.container)
	{
		Write-Information "Removing storage account '$($account.name)' container '$($container.name)' ... " -NoNewLine $true
	    if (Get-AzureStorageContainer -Context $context | ? {$_.Name -eq $container.name})
	    {
		    Remove-AzureStorageContainer -Name $container.name -Context $context -Force
		    if (!$?) {throw "Storage account '$($account.name)' container '$($container.name)' operation failed."}
			Write-Text "done."
	    }
	    else
	    {
			Write-Text "skipped."
	    }
    }
}

# Assign storage account resource group
function Assign-StorageAccountResourceGroup($account)
{
    if ($account.resourceGroup -ne $null)
    {
		Write-Information "Adding storage account '$($account.name)' resource group '$($account.resourceGroup)' ... " -NoNewLine $true
	    if (Move-ResourceItem $account.name "Microsoft.ClassicStorage/storageAccounts" $account.resourceGroup)
	    {
		    Write-Text "done."
	    }
	    else
	    {
		    Write-Text "skipped."
	    }
    }
}